-- Copyright (C) 2013 wgm.china@gmail.com (王光明)
--vss.db.method为所有list方法的操作函数
--date:2013-12-11

local strfind = string.find
local strsub  = string.sub
local strbyte = string.byte
local strrep  = string.rep
local strchar = string.char
local strrep  = string.rep
local strlen  = string.len
local type    = type
local pairs   = pairs
local tonumber= tonumber 
local table	  = table

local tb_insert = table.insert
local tb_concat = table.concat
local tb_remove = table.remove

-------1定义公共组件(开始)---------------
local data_util           = require("gcs.db.utils")
local utils		  		  = require("gcs.utils")

-------1定义公共组件（结束）----------------

module(...)
local version = '1.1'

-------2加载配置文件-----------------------

-------2加载配置文件（结束）----------------
--return version 中保存的版本号
function get_version()
	return version
end

function save_data(table_name,db_data)
	--参数必须为table
	if type(db_data) ~= "table" then
		return nil,0,"Parameter must be table format"
	end
	--local i = table.getn(db_data)
	----数据
	--if i < 1 then 
	--	return nil,"The data must be greater than or equal to 1"
	--end
	if type(table_name) ~="string" then
		return nil,0,"table_name must be string format"
	end
	
	local sql_insert = utils.fun_concat_data(" ","insert into",table_name)
	local sql_key_tmp,sql_value_tmp = {},{}
	for i,v in pairs(db_data) do
		tb_insert(sql_key_tmp,i)
		tb_insert(sql_key_tmp,",")
		tb_insert(sql_value_tmp,"'")
		tb_insert(sql_value_tmp,v)
		tb_insert(sql_value_tmp,"'")
		tb_insert(sql_value_tmp,",")
	end
	tb_remove(sql_key_tmp,#sql_key_tmp)
	tb_remove(sql_value_tmp,#sql_value_tmp)
	local sql_key = tb_concat(sql_key_tmp)
	local sql_value = tb_concat(sql_value_tmp)
	local sql = utils.fun_concat_data(" ",sql_insert,"(",sql_key,") value (",sql_value,");")
	local res,err = data_util.query(sql)
	if not res then
		return nil,0,err
	end
	local id = 0
	local res,err = data_util.query("select LAST_INSERT_ID() as id")
	if res then
		if res[1] then
			id =  tonumber(res[1].id)
		end
	end		
	return res,id,err
end

function update_data(table_name,db_data,where_data)
	--参数必须为table
	if type(db_data) ~= "table" then
		return nil,"Parameter must be table format"
	end
	--local i = table.getn(db_data)
	----数据
	--if i < 1 then 
	--	return nil,"The data must be greater than or equal to 1"
	--end
	if type(table_name) ~="string" then
		return nil,"table_name must be string format"
	end
	if not where_data then where_data = "1=1" end

	local sql_kv_tmp = {}
	for key,value in pairs(db_data) do
		tb_insert(sql_kv_tmp,key)
		tb_insert(sql_kv_tmp," = '")
		tb_insert(sql_kv_tmp,value)
		tb_insert(sql_kv_tmp,"'")
		tb_insert(sql_kv_tmp,",")		
	end
	tb_remove(sql_kv_tmp,#sql_kv_tmp)
	local sql_kv = tb_concat(sql_kv_tmp)
	local sql = utils.fun_concat_data(" ","update",table_name,"set",sql_kv,"where",where_data,";")
	
	local res,err = data_util.query(sql)
	return res,err
end


--删除数据
function del_data(table_name,where_data)
	--参数必须为table
	if type(where_data) ~= "string" then
		return nil,"Parameter must be string format"
	end
	--local i = table.getn(db_data)
	----数据
	--if i < 1 then 
	--	return nil,"The data must be greater than or equal to 1"
	--end
	if type(table_name) ~="string" then
		return nil,"table_name must be string format"
	end
	if not where_data then where_data = "1=1" end
	
	local sql = utils.fun_concat_data(" ","delete from",table_name,"where",where_data,";")
	local res,err = data_util.query(sql)
	return res,err
end


function select_data(table_name,db_data,where_data)
	--参数必须为table
	if type(db_data) ~= "table" then
		return nil,"Parameter must be table format"
	end
	--local i = table.getn(db_data)
	----数据
	--if i < 1 then 
	--	return nil,"The data must be greater than or equal to 1"
	--end
	if type(table_name) ~="string" then
		return nil,"table_name must be string format"
	end

	if not where_data then where_data = "1=1" end
	
	local sql_key_tmp = {}
	for _,v in pairs(db_data) do
		tb_insert(sql_key_tmp,v)
		tb_insert(sql_key_tmp,",")
	end
	tb_remove(sql_key_tmp,#sql_key_tmp)
	local sql_key = tb_concat(sql_key_tmp)
	local sql = utils.fun_concat_data(" ","select ",sql_key,"from",table_name,"where",where_data,";")
	local res,err = data_util.query(sql)
	return res,err
end



function query(sql)
	local res,err = data_util.query(sql)
	return res,err
end
